package com.matt.repository.photoAlbum;

import com.matt.domain.photoalbum.PhotoAlbum;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;


public interface PhotoAlbumRepository extends  JpaRepository<PhotoAlbum, String> , JpaSpecificationExecutor<PhotoAlbum> {

    PhotoAlbum getOneByAreaIdAndChildrenIdAndType(String areaId, String childrenId,Integer type);
    PhotoAlbum getOneByAreaIdAndClassIdIsNullAndTitle(String areaId, String title);

    PhotoAlbum getOneByAreaIdAndIdAndTypeAndDeletedIsFalse(String areaId, String id,Integer type);

    List<PhotoAlbum> findAllByDeletedIsFalseAndAreaIdAndIdIn(String areaId, String[] ids);

    Page<PhotoAlbum> findAllByAreaIdAndDeletedIsFalse(String areaId, Pageable pageable);
    List<PhotoAlbum> findAllByAreaIdAndTypeAndDeletedIsFalseOrderBySortDesc(String areaId,Integer type);
    List<PhotoAlbum> findAllByAreaIdAndClassIdAndTypeAndDeletedIsFalseOrderBySortDesc(String areaId, String classId,Integer type);


    @Query(value = "SELECT b.* FROM tb_photo_album b " +
            "LEFT JOIN rel_classes_children rcc on rcc.childrenId = b.childrenId " +
            "WHERE b.deleted is false AND b.aid = ?1 AND rcc.classesId = ?2 and b.type= ?3 " +
            "ORDER BY b.sort DESC", nativeQuery = true)
    List<PhotoAlbum> findAllByAreaIdAndChildrenInClass(String areaId, String classId,Integer type);

    @Query("SELECT p FROM PhotoAlbum p WHERE p.deleted = false AND p.areaId = :areaId AND p.title LIKE %:title%")
    List<PhotoAlbum> searchAlbums(@Param("areaId") String areaId, @Param("title") String title);
}
